package com.huizhonglm.diap.widget.expandTab;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.DisplayUtil;

import java.util.List;

public class MenuListAdapter extends ArrayAdapter<MenuInfo> {

	private Context mContext;
	private List<MenuInfo> mListData;
	private MenuInfo[] mArrayData;
	private int selectedPos = -1;
	private String selectedText = "";
	private int normalDrawbleID;
	private int selectedDrawbleID;
	private float textSize;
	private OnClickListener onClickListener;
	private OnItemClickListener mOnItemClickListener;
	private int paddingLeft;

	public MenuListAdapter(Context context, List<MenuInfo> listData, int sId, int nId) {
		super(context, R.layout.widget_extab_list_item, listData);
		mContext = context;
		mListData = listData;
		selectedDrawbleID = sId;
		normalDrawbleID = nId;
		init();
	}

	public MenuListAdapter(Context context, MenuInfo[] arrayData, int sId, int nId) {
		super(context, R.layout.widget_extab_list_item, arrayData);
		mContext = context;
		mArrayData = arrayData;
		selectedDrawbleID = sId;
		normalDrawbleID = nId;
		init();
	}

	private void init() {
		paddingLeft = DisplayUtil.dip2px(10f);
		onClickListener = new OnClickListener() {

			@Override
			public void onClick(View view) {
				selectedPos = (Integer) view.getTag();
				setSelectedPosition(selectedPos);
				if (mOnItemClickListener != null) {
					mOnItemClickListener.onItemClick(view, selectedPos);
				}
			}
		};
	}

	/**
	 * 设置选中的position,并通知列表刷新
	 */
	public void setSelectedPosition(int pos) {
		if (mListData != null && pos < mListData.size()) {
			selectedPos = pos;
			selectedText = mListData.get(pos).getName();
			notifyDataSetChanged();
		} else if (mArrayData != null && pos < mArrayData.length) {
			selectedPos = pos;
			selectedText = mArrayData[pos].getName();
			notifyDataSetChanged();
		}

	}

	/**
	 * 设置选中的position,但不通知刷新
	 */
	public void setSelectedPositionNoNotify(int pos) {
		selectedPos = pos;
		if (mListData != null && pos < mListData.size()) {
			selectedText = mListData.get(pos).getName();
		} else if (mArrayData != null && pos < mArrayData.length) {
			selectedText = mArrayData[pos].getName();
		}
	}

	/**
	 * 获取选中的position
	 */
	public int getSelectedPosition() {
		if (mArrayData != null && selectedPos < mArrayData.length) {
			return selectedPos;
		}
		if (mListData != null && selectedPos < mListData.size()) {
			return selectedPos;
		}

		return -1;
	}

	/**
	 * 设置列表字体大小
	 */
	public void setTextSize(float tSize) {
		textSize = tSize;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TextView view;
		if (convertView == null) {
			view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.widget_extab_list_item, parent, false);
		} else {
			view = (TextView) convertView;
		}
		view.setTag(position);
		String mString = "";
		if (mListData != null) {
			if (position < mListData.size()) {
				mString = mListData.get(position).getName();
			}
		} else if (mArrayData != null) {
			if (position < mArrayData.length) {
				mString = mArrayData[position].getName();
			}
		}

		if (mString.equals("热门推荐")){
			view.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_category_top, 0, 0, 0);
		} else {
			view.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
		}

		if (mString.contains("不限"))
			view.setText("不限");
		else
			view.setText(mString);
		view.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize);

		if (selectedText != null && selectedText.equals(mString)) {
			view.setBackgroundResource(selectedDrawbleID);//设置选中的背景图片
			view.setTextColor(ContextCompat.getColor(mContext, R.color.mainRed));
		} else {
			view.setBackgroundResource(normalDrawbleID);//设置未选中状态背景图片
			view.setTextColor(ContextCompat.getColor(mContext, R.color.yahei));
		}
		view.setPadding(paddingLeft, 0, 0, 0);
		view.setOnClickListener(onClickListener);
		return view;
	}

	public void setOnItemClickListener(OnItemClickListener l) {
		mOnItemClickListener = l;
	}

	/**
	 * 重新定义菜单选项单击接口
	 */
	public interface OnItemClickListener {
		void onItemClick(View view, int position);
	}
}
